package yl.hs.bmipfull.service.vehiclemanage.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.vehiclemanage.ITruckOwnerDao;
import yl.hs.bmipfull.pojo.vehiclemanage.TruckOwnerEntity;
import yl.hs.bmipfull.service.vehiclemanage.ITruckOwnerService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * 作用:  车主信息表服务层接口实现<br/>
 * 创建时间: 2021年06月11日 10时56分20秒 <br/>
 *
 * @author 张凯
 */
@Service
public class TruckOwnerServiceImpl extends VehicleBaseService implements ITruckOwnerService {

    @Autowired
    ITruckOwnerDao dao;

    @Override
    public List<TruckOwnerEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(TruckOwnerEntity.class);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), TruckOwnerEntity.class);
        return dao.queryPage(pageQuery.getQuery(), filters);
    }

    @Override
    public PageResult<TruckOwnerEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(TruckOwnerEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<TruckOwnerEntity> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), TruckOwnerEntity.class);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(), filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(), filters));
        return result;
    }


    @Override
    public TruckOwnerEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional
    public Integer saveTruckOwner(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        TruckOwnerEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new TruckOwnerEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(TruckOwnerEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入车主信息表信息", Tl.toJson(entity), TruckOwnerEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            var changelog = Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, changelog, Tl.toJson(entity), TruckOwnerEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, TruckOwnerEntity.class);
        }
        return result;
    }

    @Override
    public Integer recovery(String[] sn, short state) {
        Integer result = dao.changeStateBySN(sn, state);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, TruckOwnerEntity.class);
        }
        return result;
    }

    @Override
    public List<TruckOwnerEntity> getSelect() {
        return dao.getSelect();
    }
}